home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / testdir.zip / TESTDIR.C < prev    next >
Text File  |  1989-01-10  |  4KB  |  189 lines

  1. /*
  2.     Testdir.c
  3.  
  4.     Display a 'tree' of the directory structure for a given drive.
  5.     Include drive name as a command line argument to specify drive. If
  6.     not specified, the current logged drive is mapped. This was written
  7.     to develop and test the techniques to display a graphic tree
  8.     structure for a commercial product, the KYSS!(tm) System Organizer.
  9.     A number which indicates the nesting level is printed on each line.
  10.  
  11.     Written for Microsoft C V5.1
  12.  
  13.     Compile as follows:
  14.  
  15.         cl testdir.c /link /STACK:4096
  16.  
  17.     Use a larger value for stack if you run into 'Stack overflow' errors.
  18.  
  19.     The DisplayDirectories() function is recursive. A maximum of 32 levels
  20.     of sub-directory nesting can be displayed, assuming that recursion
  21.     does not eat all the memory...I've tested to 7 levels deep.
  22.  
  23.     This code is placed into the public domain by the author:
  24.     Gene McManus
  25.     The Information People
  26.     358 Grandview Rd.
  27.     Newark, OH 43055
  28.     (614) 349-8644
  29.  
  30.     It may be used for any purpose whatsoever, with no claims or
  31.     warranties made.
  32. */
  33.  
  34. #include <stdio.h>
  35. #include <dos.h>
  36.  
  37. #define TRUE    1
  38. #define FALSE    0
  39.  
  40. typedef struct find_t FILEINFO;
  41.  
  42. main(int argc, char *argv[])
  43.     {
  44.     int indent = 0;
  45.     char drive[20];
  46.     void DisplayDirectories();
  47.  
  48.     if(argc > 1)
  49.         strcpy(drive, argv[1]);
  50.     else
  51.         drive[0] = '\0';
  52.  
  53.     DisplayDirectories(indent, drive, 0);
  54.     }
  55.  
  56. /*
  57.     DisplayDirectories() - Display all subdirectories in the current
  58.     directory. If a sub-directory has sub-directories, display them.
  59.     This function is recursive.
  60. */
  61.  
  62. static unsigned char map[32];
  63.  
  64. void 
  65. DisplayDirectories(int indent, char *path, int sw)
  66.     {
  67.    static char indent_char[][4] = { " │ ", " ├─", " └─" };
  68.     static first = 1;
  69.     int done, empty_dir, i, must_close, shift;
  70.     char display[80];
  71.     char this_dir[65];
  72.     char full_path[65];
  73.     char *p;
  74.     FILEINFO FileInfo1, FileInfo2;
  75.     char *printPaths();
  76.  
  77.     if(first)
  78.         {
  79.         first = 0;
  80.         printf("0 %s\\ (Root)\n", path);
  81.         strcpy(this_dir, path);
  82.         strcat(this_dir, "\\");
  83.         strcpy(full_path, this_dir);
  84.         strcat(full_path, "*.*");
  85.         for(i = 0; i < 32; i++)
  86.             map[i] = '\0';
  87.         }
  88.     else
  89.         {
  90.         strcpy(this_dir, path);
  91.         strcpy(full_path,path);
  92.         if(full_path[strlen(full_path) - 1] != '\\')
  93.             strcat(full_path,"\\*.*");
  94.         else
  95.             strcat(full_path,"*.*");
  96.         }
  97.  
  98.     shift = indent;
  99.     must_close = FALSE;
  100.     empty_dir = TRUE;
  101.  
  102.     done = _dos_findfirst(full_path, _A_SUBDIR, &FileInfo2);
  103.     ++shift;
  104.     display[0] = '\0';
  105.  
  106.     while(!done)
  107.         {
  108.         if((FileInfo2.attrib & _A_SUBDIR) &&
  109.              FileInfo2.name[0] != '.')
  110.         /* do this only for sub-directories */
  111.             {
  112.             empty_dir = FALSE;
  113.             must_close = TRUE; 
  114.             display[0] = '\0';
  115.  
  116.             if(sw)        /* previous level ended branch */
  117.                 map[shift-1] = 0xff;
  118.  
  119.             for(i = 1; i < shift; i++)
  120.                 if(map[i] != 0xff)
  121.                     strcat(display, indent_char[0]);
  122.                 else
  123.                     strcat(display, "   ");
  124.  
  125.  
  126.             memcpy((char *)&FileInfo1, (char *)&FileInfo2, sizeof(FILEINFO));
  127.             done = _dos_findnext(&FileInfo2);
  128.             if(!done)        /* another file found in this directory */
  129.                 {
  130.                 while(!done)
  131.                     {
  132.                     if(FileInfo2.attrib & _A_SUBDIR)
  133.                         {
  134.                         must_close = FALSE;
  135.                         break;
  136.                         }
  137.                     done = _dos_findnext(&FileInfo2);
  138.                     } /* while(!done &&.. */
  139.  
  140.                 if(must_close)
  141.                 /* there are no more sub-directories */
  142.                     p = indent_char[2];
  143.                 else
  144.                 /* there is at least one more sub-directory */
  145.                     p = indent_char[1];
  146.  
  147.                 strcpy(full_path,
  148.                          printPaths(display, p, FileInfo1.name, this_dir, shift));
  149.                 DisplayDirectories(shift, full_path, must_close); 
  150.                 }
  151.             else
  152.                 {
  153.                 strcpy(full_path,
  154.                          printPaths(display, indent_char[2], FileInfo1.name, this_dir, shift));
  155.                 DisplayDirectories(shift, full_path, must_close); 
  156.                 return;
  157.                 }
  158.             }
  159.         else
  160.             {
  161.             if(must_close)
  162.                 {
  163.                 strcpy(full_path,
  164.                     printPaths(display, indent_char[2], FileInfo1.name, this_dir, shift));
  165.                 DisplayDirectories(shift, full_path, must_close); 
  166.                 return;
  167.                 }
  168.  
  169.             done = _dos_findnext(&FileInfo2);
  170.             }
  171.         }    /* while (done == 0) */
  172.     map[shift] = '\0';
  173.     }
  174.  
  175. char
  176. *printPaths(char *display, char *indent_char, char *name, char *path, int shift)
  177.     {
  178.     char full_path[65];
  179.  
  180.     strcat(display, indent_char);
  181.     strcat(display, name);
  182.     strcpy(full_path, path);
  183.     if(full_path[strlen(full_path) - 1] != '\\')
  184.         strcat(full_path, "\\");
  185.     strcat(full_path, name);
  186.     printf("%d %s \n", shift, display);
  187.     return(full_path);
  188.     }
  189.